﻿#region << 版 本 注 释 >>

/*----------------------------------------------------------------
* 类 名 称 ：ObjectExtension
* 类 描 述 ：
* 作    者 ：henrychang
* 创建时间 ：2019/1/6 20:29:01
*******************************************************************
* Copyright @ henrychang 2019. All rights reserved.
*******************************************************************
//----------------------------------------------------------------*/

#endregion << 版 本 注 释 >>

using UnityEngine;

public static class ObjectExtension
{
    #region Instantiate

    public static T Instantiate<T>(this T selfObj) where T : Object
    {
        return Object.Instantiate(selfObj);
    }

    #endregion Instantiate

    #region Instantiate

    public static T Name<T>(this T selfObj, string name) where T : Object
    {
        selfObj.name = name;
        return selfObj;
    }

    #endregion Instantiate

    #region Destroy Self

    public static void DestroySelf<T>(this T selfObj) where T : Object
    {
        Object.Destroy(selfObj);
    }

    public static T DestroySelfGracefully<T>(this T selfObj) where T : Object
    {
        if (selfObj)
        {
            Object.Destroy(selfObj);
        }

        return selfObj;
    }

    #endregion Destroy Self

    #region Destroy Self AfterDelay

    public static T DestroySelfAfterDelay<T>(this T selfObj, float afterDelay) where T : Object
    {
        Object.Destroy(selfObj, afterDelay);
        return selfObj;
    }

    public static T DestroySelfAfterDelayGracefully<T>(this T selfObj, float delay) where T : Object
    {
        if (selfObj)
        {
            Object.Destroy(selfObj, delay);
        }

        return selfObj;
    }

    #endregion Destroy Self AfterDelay

    #region Apply Self To

    public static T ApplySelfTo<T>(this T selfObj, System.Action<T> toFunction) where T : Object
    {
        toFunction.InvokeGracefully(selfObj);
        return selfObj;
    }

    #endregion Apply Self To

    #region DontDestroyOnLoad

    public static T DontDestroyOnLoad<T>(this T selfObj) where T : Object
    {
        Object.DontDestroyOnLoad(selfObj);
        return selfObj;
    }

    #endregion DontDestroyOnLoad

    public static T As<T>(this Object selfObj) where T : Object
    {
        return selfObj as T;
    }

    public static T LogInfo<T>(this T selfObj, string msgContent) where T : Object
    {
        Debug.Log(msgContent);
        return selfObj;
    }
}